var repairRecordId = getUrlParam("repairRecordId");
$(function(){
	
	Server.getFromService("/getRepairRecord",{id:repairRecordId},function(result){
		if (result.code === 200) {
			var data = result.data.repairRecord;
			$("input[type=text]").each(function(index,element){
				$("#"+element.id).val(data[element.id]);
			})
//			if (result.data.sparepartFee.length != 0 ) {
//				var tempData = result.data.sparepartFee;
//				var str = '<div class="cell-item"><div class="cell-left">所需配件</div><input class="cell-right" type="text" disabled="disabled" value="'+ tempData[0].sparepartName +'" /></div>';
//				for (var i = 1; i < result.data.sparepartFee.length; i++) {
//					str += '<div class="cell-item"><div class="cell-left"></div><input class="cell-right" type="text" disabled="disabled" value="'+ tempData[i].sparepartName +'" /></div>';
//				}
//				$("#sparepartFees").append(str);
//			}
		}
		
	})
});

$("#submitSign").click(function(){

	var canvas = document.getElementById("canvasId");
	
	var dataUrl = canvas.toDataURL("image/jpeg",1.0); //canvas转换为dataURL

	console.log(dataUrl);
	var fileName = new Date().getTime()+"sign.png";
	
	var file = dataURLtoFile(dataUrl,fileName);
	
	var param = {
		file:file,
		name: fileName,
		success:function(err, data){
			if (err) {
				layer.msg('上传失败！');
				return;
			}
		},
		curTiem:new Date().getTime()
	}
	
	Server.postToService("/saveSign",{id:repairRecordId,sign:fileName},function(result){
		
		if (result.code === 200) {
			uploadToCos(param);
			layer.msg('保存成功！');
		}
		
	})
	console.log(fileName);
	
});
//将dataUrl转换为File对象
function dataURLtoFile(dataurl, filename) {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, {type:mime});
}
